package com.example.mobilesafe.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.TextView;

import com.example.mobilesafe.R;
import com.example.mobilesafe.callback.SmsBackUpCallBack;
import com.example.mobilesafe.dao.SmsBackUpDao;

import java.io.File;

public class AToolActivity extends AppCompatActivity {

    private TextView tv_query_phone_address;

    private TextView tv_sms_backup;

    private TextView tv_commonnumber_query;

    private TextView tv_app_lock;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_a_tool);

        // 1.电话归属地查询方法
        initPhoneAddress();

        // 2.短信备份方法
        initSmsBackUp();

        // 3.常用号码查询
        initCommonNumberQuery();

        // 4.程序锁
        initAppLock();
    }

    /**
     * 1.电话归属地查询方法
     */
    private void initPhoneAddress() {
        tv_query_phone_address = findViewById(R.id.tv_query_phone_address);
        tv_query_phone_address.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getApplicationContext(),QueryAddressActivity.class));
            }
        });
    }

    /**
     * 2.短信备份方法
     */
    private void initSmsBackUp() {
        tv_sms_backup = findViewById(R.id.tv_sms_backup);
        tv_sms_backup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               showSmsBackUpDialog();
            }
        });
    }

    /**
     * 展示备份短信的进度条的对话框
     */
    private void showSmsBackUpDialog() {
        // 1.创建一个带进度条的对话框
        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setIcon(R.drawable.ic_launcher);
        progressDialog.setTitle("短信备份");
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); // 指定进度条的样式为水平
        progressDialog.show(); // 展示进度条
        // 2.获取短信
        new Thread(){
            @Override
            public void run() {
                String path = getApplicationContext().getExternalFilesDir(null) + File.separator + "smsbackup.xml"; // 将短信信息存储到SD卡中
                SmsBackUpDao.backUp(getApplicationContext(), path, new SmsBackUpCallBack() {
                    @Override
                    public void setMax(int max) {
                        progressDialog.setMax(max);
                        // 进度条.setMax(max);
                    }

                    @Override
                    public void setProgress(int index) {
                        progressDialog.setProgress(index);
                        // 进度条.setProgress(index);
                    }
                });
                progressDialog.dismiss();
            }
        }.start();
    }

    /**
     * 3.常用号码查询
     */
    private void initCommonNumberQuery() {
        tv_commonnumber_query = findViewById(R.id.tv_commonnumber_query);
        tv_commonnumber_query.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getApplicationContext(),CommonNumberQueryActivity.class));
            }
        });
    }

    /**
     * 4.程序锁
     */
    private void initAppLock() {
        tv_app_lock = findViewById(R.id.tv_app_lock);
        tv_app_lock.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getApplicationContext(),AppLockActivity.class));
            }
        });
    }
}